package com.lzy.system.organization.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzy.conf.JSONResult;
import com.lzy.system.organization.dto.SysCompanyDTO;
import com.lzy.system.organization.service.ISysCompanyService;
import com.lzy.utils.ValidList;
import com.lzy.utils.validator.group.ServiceInsert;
import com.lzy.utils.validator.group.ServiceUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 公司信息管理
 * </p>
 *
 * @author lcc
 * @since 2022-08-13
 */
@RestController
@RequestMapping("/organization/sysCompany")
@Api(tags = "公司信息管理")
@Slf4j
public class SysCompanyController {
    ISysCompanyService iSysCompanyService;

    @Autowired
    public SysCompanyController(ISysCompanyService iSysCompanyService) {
        this.iSysCompanyService = iSysCompanyService;
    }

    @ApiOperation("添加公司")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JSONResult save(@ApiParam("公司信息") @RequestBody @Validated(value = {ServiceInsert.class}) ValidList<SysCompanyDTO> sysCompanyDTOS) throws Exception {
        return JSONResult.ok(iSysCompanyService.addCompany(sysCompanyDTOS));
    }

    @ApiOperation("修改用户")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JSONResult update(
            @ApiParam("公司信息") @RequestBody @Validated(value = {ServiceUpdate.class}) ValidList<SysCompanyDTO> sysCompanyDTOS
    ) throws Exception {
        return JSONResult.ok(iSysCompanyService.updateCompany(sysCompanyDTOS));
    }

    @ApiOperation("删除公司")
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public JSONResult del(
            @ApiParam("用户唯一标识，多个,逗号间隔") @RequestParam(name = "ids", required = true) String ids
    ) throws Exception {
        return JSONResult.ok(iSysCompanyService.delById(ids));
    }


    @ApiOperation("查询公司列表")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public JSONResult getList(
            @ApiParam("公司名称") @RequestParam(name = "company_name", required = false, defaultValue = "") String company_name,
            @ApiParam("公司编码") @RequestParam(name = "company_no", required = false, defaultValue = "") String company_no,
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "5") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) throws Exception {
        return JSONResult.ok(iSysCompanyService.getCompanyList(company_name, company_no, limit, page));
    }

    @ApiOperation("查询公司列表")
    @RequestMapping(value = "getListAll", method = RequestMethod.POST)
    public JSONResult getListAll(
    ) throws Exception {
        return JSONResult.ok(iSysCompanyService.list(new QueryWrapper(){{
            orderByDesc("is_def");
            orderByAsc("orderindex");
            orderByAsc("create_date");
        }}));
    }

    @ApiOperation("设置默认公司")
    @RequestMapping(value = "setDef/{id}", method = RequestMethod.POST)
    public JSONResult setDef(
            @PathVariable(value = "id",required = true) int id
    ) throws Exception {
        return JSONResult.ok(iSysCompanyService.setDef(id));
    }

}
